package com.moxiao.yfjh.module.activity.controller.admin.merchantInfo;

import com.moxiao.yfjh.module.activity.common.GenActiveNoCommonUtil;
import com.moxiao.yfjh.module.activity.controller.admin.merchanType.vo.MerchantTypeExportReqVO;
import com.moxiao.yfjh.module.activity.dal.dataobject.merchanType.MerchantTypeDO;
import com.moxiao.yfjh.module.activity.service.merchanType.MerchantTypeService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;

import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;

import com.moxiao.yfjh.module.activity.controller.admin.merchantInfo.vo.*;
import com.moxiao.yfjh.module.activity.dal.dataobject.merchantInfo.MerchantInfoDO;
import com.moxiao.yfjh.module.activity.convert.merchantInfo.MerchantInfoConvert;
import com.moxiao.yfjh.module.activity.service.merchantInfo.MerchantInfoService;

@Tag(name = "管理后台 - 相亲商家信息")
@RestController
@RequestMapping("/activity/merchant-info")
@Validated
public class MerchantInfoController {

    @Resource
    private MerchantInfoService merchantInfoService;
    @Resource
    private GenActiveNoCommonUtil genActiveNoCommonUtil;

    @Resource
    private MerchantTypeService merchantTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建相亲商家信息")
    @PreAuthorize("@ss.hasPermission('activity:merchant-info:create')")
    public CommonResult<Long> createMerchantInfo(@Valid @RequestBody MerchantInfoCreateReqVO createReqVO) {
        genActiveNoCommonUtil.addMerchantInfoNo(createReqVO);
        return success(merchantInfoService.createMerchantInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲商家信息")
    @PreAuthorize("@ss.hasPermission('activity:merchant-info:update')")
    public CommonResult<Boolean> updateMerchantInfo(@Valid @RequestBody MerchantInfoUpdateReqVO updateReqVO) {
        merchantInfoService.updateMerchantInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲商家信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('activity:merchant-info:delete')")
    public CommonResult<Boolean> deleteMerchantInfo(@RequestParam("id") Long id) {
        merchantInfoService.deleteMerchantInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲商家信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('activity:merchant-info:query')")
    public CommonResult<MerchantInfoRespVO> getMerchantInfo(@RequestParam("id") Long id) {
        MerchantInfoDO merchantInfo = merchantInfoService.getMerchantInfo(id);
        MerchantInfoRespVO merchantInfoRespVO = MerchantInfoConvert.INSTANCE.convert(merchantInfo);
        MerchantTypeDO merchantType = merchantTypeService.getMerchantType(merchantInfo.getMerchantType());
        if (Objects.nonNull(merchantType)){
            merchantInfoRespVO.setMerchantTypeName(merchantType.getName());
        }
        return success(merchantInfoRespVO);
    }

    @GetMapping("/getMerchantTypeList")
    @Operation(summary = "获得相亲商家类型列表")
    public CommonResult<List<MerchantTypeDO>> getMerchantTypeList() {
        return success(merchantTypeService.getMerchantTypeList(new MerchantTypeExportReqVO()));
    }

    @GetMapping("/getMerchantInfoList")
    @Operation(summary = "获得相亲商家列表")
    public CommonResult<List<MerchantInfoDO>> getMerchantInfoList(){
        return success(merchantInfoService.getMerchantInfoList(new MerchantInfoExportReqVO()));
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲商家信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('activity:merchant-info:query')")
    public CommonResult<List<MerchantInfoRespVO>> getMerchantInfoList(@RequestParam("ids") Collection<Long> ids) {
        List<MerchantInfoDO> list = merchantInfoService.getMerchantInfoList(ids);
        return success(MerchantInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲商家信息分页")
    @PreAuthorize("@ss.hasPermission('activity:merchant-info:query')")
    public CommonResult<PageResult<MerchantInfoRespVO>> getMerchantInfoPage(@Valid MerchantInfoPageReqVO pageVO) {
        PageResult<MerchantInfoDO> pageResult = merchantInfoService.getMerchantInfoPage(pageVO);
        PageResult<MerchantInfoRespVO> merchantInfoRespVOPageResult = MerchantInfoConvert.INSTANCE.convertPage(pageResult);
        if (Objects.nonNull(merchantInfoRespVOPageResult)){
            List<MerchantInfoRespVO> list = merchantInfoRespVOPageResult.getList();
            if (CollectionUtils.isNotEmpty(list)){
                list.forEach(merchantInfoRespVO -> {
                    MerchantTypeDO merchantType = merchantTypeService.getMerchantType(merchantInfoRespVO.getMerchantType());
                    if (Objects.nonNull(merchantType)){
                        merchantInfoRespVO.setMerchantTypeName(merchantType.getName());
                    }
                });
            }
        }
        return success(merchantInfoRespVOPageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲商家信息 Excel")
    @PreAuthorize("@ss.hasPermission('activity:merchant-info:export')")
    @OperateLog(type = EXPORT)
    public void exportMerchantInfoExcel(@Valid MerchantInfoExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MerchantInfoDO> list = merchantInfoService.getMerchantInfoList(exportReqVO);
        // 导出 Excel
        List<MerchantInfoExcelVO> datas = MerchantInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲商家信息.xls", "数据", MerchantInfoExcelVO.class, datas);
    }

}
